package com.ymatou.iapi.prodstock.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prodstock.parameter.RefreshProductStockBean;
import com.ymatou.iapi.prodstock.service.RefreshProductStockCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.CatalogSaveModel;
import com.ymatou.iapi.productprice.parameter.ProductAllPriceBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ActivityCallService;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsInActivity;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
/**
*刷新商品总库存
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RefreshProductStock{
	private static RefreshProductStockBean refreshproductstockBean;
	private static RefreshProductStockCall refreshproductstockCall;
	private static String sellerName = EnvSetup.getData("autotest_sellerName1");
	private static int sellerid = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("刷新商品总库存");
	}

	@Before
	public void caseUp() {
		refreshproductstockBean=new RefreshProductStockBean();
		refreshproductstockCall=new RefreshProductStockCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(refreshproductstockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 刷新库存暂时用不了
	 */
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RefreshProductStock_001() {
		Logger.start(true,"刷新商品总库存成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerid);
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			refreshproductstockBean.setAppId("test");
			refreshproductstockCall.setData(refreshproductstockBean);
			refreshproductstockCall.callService();	
			Logger.verifyEquals("ok", refreshproductstockCall.getReturnData(), "验证ReturnData");
			Thread.sleep(5000);
			
			Integer stock = new YmtProductsIWapper().getiTotalStock(ProductId);
			Integer Stocksum=addpro.getProduct().getCatalogList().stream().mapToInt(CatalogSaveModel::getStock).sum();
			Logger.verifyEquals(Stocksum,stock,"验证Stock");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RefreshProductStock_002() {
		Logger.start(true,"非交易隔离刷新商品总库存成功");
		try {
//			int sellerid=288484;
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerid);
			addpro.getProduct().getCatalogList().remove(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
//			ProductPriceCallService.getProdPriceWithActivity(sellerid, productI d, MapUtil.hashMap(
//					"ActivityTemplateId", 1));
//			new ActivityProductsWapper().update(productId, MapUtil
//					.hashMap("part", true, "nbuyer", true, "isolation", false,
//							"mlimits", new ArrayList<>()));
			Integer activityId=ActivityCallService.createNoIsolationActivityInProcess();
			Logger.comment("创建非交易隔离活动:"+activityId);
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Integer anum=1;
			//ProductManagerCallServiceV2.addActivity(productId, sellerid, clist, activityId,anum);
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerid,catalogs,activityId, anum,"10");
			//审核通过
			
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			refreshproductstockBean.setAppId("123");
			refreshproductstockCall.setData(refreshproductstockBean);
			refreshproductstockCall.callService();			
			Logger.verifyEquals("ok", refreshproductstockCall.getReturnData(), "验证ReturnData");
			Thread.sleep(5000);
			Integer stock = new YmtProductsIWapper().getiTotalStock(productId);
			Integer Stocksum=addpro.getProduct().getCatalogList().stream().mapToInt(CatalogSaveModel::getStock).sum();
			//Logger.verifyEquals(Stocksum-anum,stock,"验证Stock");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RefreshProductStock_003() {
		Logger.start(true,"交易隔离刷新商品总库存");
		try {
//			int sellerid=288484;
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerid);
			addpro.getProduct().getCatalogList().remove(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品:"+productId);
			Integer activityId=ActivityCallService.createIsolationActivityInProcess();
			Logger.comment("创建交易隔离活动:"+activityId);
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Integer anum=1;
			//ProductManagerCallServiceV2.addActivity(productId, sellerid, Arrays.asList(catalogsID), activityId,anum);

			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerid,catalogs,activityId, anum,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			refreshproductstockCall.setData(refreshproductstockBean);
			refreshproductstockCall.callService();			
			Logger.verifyEquals("ok", refreshproductstockCall.getReturnData(), "验证ReturnData");
			Thread.sleep(5000);
			Integer stock = new YmtProductsIWapper().getiTotalStock(productId);
			Integer Stocksum=addpro.getProduct().getCatalogList().stream().mapToInt(CatalogSaveModel::getStock).sum();
			//扣减库存
			//Logger.verifyEquals(Stocksum-anum,stock,"验证Stock-交易隔离计算");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RefreshProductStock_004() {
		Logger.start(true,"多规格非交易隔离修改库存后刷新商品总库存成功");
		try {
//			int sellerid=288484;
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerid);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Integer activityId=ActivityCallService.createNoIsolationActivityInProcess();
			Logger.comment("创建非交易隔离活动:"+activityId);
			List clist = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Integer anum=1;
			Logger.debug(clist);
			List<String> catalogs1 = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerid,catalogs1,activityId, anum,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);

			//ProductManagerCallServiceV2.addActivity(productId, sellerid, clist, activityId,anum);

			 YmtCatalogsI catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0);
			Logger.comment("修改库存-1");
			ProductManagerCallServiceV2.updateProductStockNum(sellerid, productId,
					catalogs.getsCatalogId(), catalogs.getiNum()-1);
			refreshproductstockCall.setData(refreshproductstockBean);
			refreshproductstockCall.callService();			
			Logger.verifyEquals("ok", refreshproductstockCall.getReturnData(), "验证ReturnData");
			Thread.sleep(5000);
			Integer stock = new YmtProductsIWapper().getiTotalStock(productId);
			//创建的总库存
			Integer Stocksum=addpro.getProduct().getCatalogList().stream().mapToInt(CatalogSaveModel::getStock).sum();
			//2个规格报名1 + 修改库存1
			//Logger.verifyEquals(Stocksum-3,stock,"验证Stock");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}